package vashion.azeroth.core.item.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import vashion.azeroth.common.math.BigDecimalUtil;
import vashion.azeroth.common.util.ItemImageUtils;
import vashion.azeroth.core.activity.ActiveConstant;
import vashion.azeroth.core.activity.GlobalActivityExtendInfoDO;
import vashion.azeroth.core.pojo.BaseObject;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by tingting on 15/8/31.
 * 商品对象
 */
@Data
public class ItemDO extends BaseObject {

    private static final long serialVersionUID = 1L;
    public static final String DESC_PIC_SPLIT = ",";
    /**
     * 主键
     */
    private long id;

    /**
     * 创建时间
     */
    private Date gmtCreate;

    /**
     * 修改时间
     */
    private Date gmtModified;

    /**
     * 标题、描述或者标签修改时间
     */
    private Date specialGmtModified;

    /**
     * 创建者Id
     */
    private long userId;
    
    /**
     * 创建者账号
     */
    private String memberName;
    
    /**
     * 标题
     */
    private String title;

    /**
     * 图片,商品主图
     */
    private String pic;

    /**
     * 商品的其他图片，以,分隔
     */
    private String descPic;

    /**
     * 类目关税
     */
    private BigDecimal tariff;

    /**
     * 原价，分单位
     */
    private long price;

    /**
     * 查考价，分单位
     */
    private long referencePrice;
    
    /**
     * 用于显示的价格，元单位
     */
    private String priceDisplay;
    
    /**
     * 用于显示的查考价格，元单位
     */
    private String referencePriceDisplay;
    
    /**
     * 价格的货币代码
     */
    private String priceCurno;

    /**
     * 会员价
     */
    private long vipPrice;

    /**
     * 商品描述
     */
    private String itemDesc;

    /**
     * 类目Id
     */
    private long categoryId;

    /**
     * 类目的名字，包括父类目的名字
     */
    private String categoryFullTitle;

    /**
     * 商品feature,受众群体
     */
    private String feature;

    /**
     * 商品状态
     */
    private Integer status;

    /**
     * 商品的总库存
     */
    private int stock;

    /**
     *
     **/
    private long tag1;

    /**
     *
     **/
    private long tag2;

    /**
     *
     **/
    private long tag3;

    /**
     * 商品编码
     **/
    private String itemCode;
    
    /**
     * 商品编码图片
     **/
    private String itemCodePic;

    /**
     * 邮费
     */
    private long postage;

    /**
     * 佣金
     */
    private long commission;

    /**
     * 返现
     */
    private long cashBack;

    /**
     * 商品销量
     */
    private int saleCount;
    
    /**
     * 品牌Id
     */
    private long brandId;

    /**
     * 品牌标题
     */
    private String brandTitle;
    
    /**
     * 商品材质
     **/
    private String material;
    
    /**
     * 商品季节信息
     **/
    private String season;
    
    /**
     * 商品spu码
     **/
    private String spu;

    /**
     * 商品对应的sku信息
     */
    private List<ItemSkuDO> itemSkuDOList;

    /**
     * 商品销售属性Map
     */
    private List<PropertyValueDO> featureValueDOList;

    /**
     * 给ios用协议转换用的Id
     */
    private long iosId;

    /**
     * 商品帖子Id，业务需要需要，db未存储
     */
    private long publishFileId;

    //--图片底层存储相对路径，上层总cdn前缀补充--

    public void setPic(String pic) {
    	this.pic = pic;
        //this.pic = ItemImageUtils.removeImgCdnPrefix(pic);
    }

    public String getPic() {
    	return pic;
        //return ItemImageUtils.getImgFullPath(pic);
    }

    public String getDescPic() {
        return descPic;
    }

    public void setDescPic(String descPic) {
        if (StringUtils.isBlank(this.descPic)) {
            this.descPic = descPic;
        }
        StringBuilder stringBuilder = new StringBuilder("");
        for (String splitStr : this.descPic.split(DESC_PIC_SPLIT)) {
            stringBuilder.append(ItemImageUtils.removeImgCdnPrefix(splitStr) + DESC_PIC_SPLIT);
        }
        String descPicTemp = stringBuilder.toString();
        if (descPicTemp.endsWith(DESC_PIC_SPLIT)) {
        	descPicTemp = descPicTemp.substring(0, descPicTemp.length() - 1);
        }
        this.descPic = descPicTemp;
    }

    /**
     * 商品的其他图片 list格式
     *
     * @return
     */
    public List<String> getDescPicList() {
        if (StringUtils.isBlank(this.descPic)) {
            return Lists.newArrayList();
        }
        List<String> imageList = Lists.newArrayList();
        for (String split : this.descPic.split(DESC_PIC_SPLIT)) {
            imageList.add(split);
        }
        return imageList;
    }


    public long getIosId() {
        return id;
    }

    public long getVipPrice() {
        return vipPrice > 0 ? vipPrice : price;
    }

    public List<Long> getFeatureIdList() {
        List<Long> idList = Lists.newArrayList();
        if (StringUtils.isBlank(this.feature) || !this.feature.contains(",")) {
            return idList;
        }
        for (String child : feature.split(",")) {
            long id = 0;
            try {
                id = NumberUtils.toLong(child.split(":")[1]);
            } catch (Throwable e) {

            }
            if (id > 0) {
                idList.add(id);
            }
        }
        return idList;
    }

    public static ItemDO buildByActivity(GlobalActivityExtendInfoDO globalActivityExtendInfoDO) {
        ItemDO itemDO = new ItemDO();
        Map<String, String> jsonMap = JSON.parseObject(globalActivityExtendInfoDO.getContents(), new TypeReference<Map<String, String>>() {
        });
        if (!jsonMap.containsKey(ActiveConstant.SCHEMA_ITEM_ID) ||
                !jsonMap.containsKey("itemTitle") ||
                !jsonMap.containsKey("itemPic")) {
            return null;
        }
        itemDO.setId(NumberUtils.toLong(jsonMap.get(ActiveConstant.SCHEMA_ITEM_ID)));
        itemDO.setTitle(jsonMap.get("itemTitle"));
        itemDO.setPic(jsonMap.get("itemPic"));
        return itemDO;
    }
    
    public void setPriceDisplay(String priceDisplay) {
    	this.price = BigDecimalUtil.multiply(priceDisplay, "100").longValue();
    }
    
    public String getPriceDisplay() {
    	return String.valueOf(BigDecimalUtil.divide(new BigDecimal(price), new BigDecimal(100)));
    }
    
    public void setReferencePriceDisplay(String referencePriceDisplay) {
    	this.referencePrice = BigDecimalUtil.multiply(referencePriceDisplay, "100").longValue();
    }
    
    public String getReferencePriceDisplay() {
    	return String.valueOf(BigDecimalUtil.divide(new BigDecimal(referencePrice), new BigDecimal(100)));
    }
    
    public void setItemCode(String itemCode) {
    	this.itemCode = itemCode.replaceAll("[^0-9a-zA-Z]", "");
    }
}
